<?php
/*
 *后台登录
 */
namespace App\Http\Controllers\Admin;
//引入controller控制器
use App\Http\Controllers\Controller;
//引入请求类
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
//引入model类
use App\Models\Test;
use Gregwar\Captcha\CaptchaBuilder;//引入验证码类
use Illuminate\Support\Facades\Session;//引入session文件 
use Illuminate\Support\Facades\Redis;//引入redis
Class LoginController extends Controller
{
    /*
     *登录页面
     */
    public function login(Request $request)
    {
    	if($request->isMethod('post')){
    		//接收发送过来的值
    		$data = Input::all();
    		if(!empty($data['code'])){
    			//取出验证码
    			$code = Session::get('checkcode');
    			if($data['code']==$code){
    				//md5进行加密
                    // echo 1;die;
					$data['login_name'] = md5($data['login_name']);
		    		$data['login_pwd'] = md5($data['login_pwd']);
		    		if(!empty($data['_token'])){
		    			unset($data['_token']);
		    			unset($data['code']);
		    			//实例化model类
		    			$model = new Test;
		    			//调用方法
		    			$test_query = $model->test_query('admin_login',$data);
		    			if($test_query){
		    				//整条数据存入cookie
		    				$val = json_encode($test_query);
                            //实例化redis
                            $name = Redis::set('name', $val);  
                            if($name){
                                echo "<script>alert('登录成功');location.href='/admin/index';</script>";
                            }
                        }
    				}
    			}
    		}
    	}
    		//渲染
        	return view('admin.login');
    }

    //形成验证码方法
    public function getCheckCode()
    {
        //创建验证码对象
        $builder = new CaptchaBuilder;
        $builder->build($width = 100, $height = 40,$font = null);
        $phrase = $builder->getPhrase();
        Session::flash("checkcode", $phrase);
        header("Cache-Control: no-cache, must-revalidate");
        header("Content-Type: image/jpeg");
        return $builder->output();
    }
}